package com.tianyadu.apigateway.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import com.tianyadu.apigateway.constant.RedisConstant;
import com.tianyadu.apigateway.util.CookieUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_DECORATION_FILTER_ORDER;
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_TYPE;

/**
 * @FileName: AuthFilter
 * @Description: 用户权限拦截校验
 * @author: xueyj
 * @create: 2018-11-26 18:04
 */
@Component
public class AuthFilter extends ZuulFilter{

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
      * @Description: 拦截类型：pre
      * @param:
      * @return:
      * @author:
      * @date:   2018/11/26 18:06
      */
    @Override
    public String filterType() {
        return PRE_TYPE;
    }

    /**
      * @Description: 拦截级别
      * @param:
      * @return:
      * @author:
      * @date:   2018/11/26 18:07
      */
    @Override
    public int filterOrder() {
        return PRE_DECORATION_FILTER_ORDER - 1;
    }

    /**
      * @Description: 是否开启过滤
      * @param:
      * @return:
      * @author:
      * @date:   2018/11/26 18:07
      */
    @Override
    public boolean shouldFilter() {
        return false;
    }

    /**
      * @Description: 拦截信息校验
      * @param:
      * @return:
      * @author:
      * @date:   2018/11/26 18:07
      */
    @Override
    public Object run() throws ZuulException {
        RequestContext requestContext = RequestContext.getCurrentContext();
        HttpServletRequest request = requestContext.getRequest();
       /**
            /order/create  只能买家访问
            /order/finish   只能卖家访问
            /product/list   都可以访问
         */
       // 校验买家信息
       if ("/order/order/create".equals(request.getRequestURI())) {
           //   获取cookie信息
           Cookie cookie = CookieUtil.get(request, "openid");
           if (cookie == null || StringUtils.isEmpty(cookie.getValue())) {
               //   设置ZuulResponse为false
               requestContext.setSendZuulResponse(false);
               //   设置请求状态码信息（401）
               requestContext.setResponseStatusCode(HttpStatus.UNAUTHORIZED.value());
           }
       }

        //  校验卖家信息
        if ("/order/order/finish".equals(request.getRequestURI())) {
            //   获取cookie信息
            Cookie cookie = CookieUtil.get(request, "token");
            String redisCookieValue = redisTemplate.opsForValue().get(String.format(RedisConstant.TOKEN_TEMPLATE, cookie.getValue()));

            if (cookie == null || StringUtils.isEmpty(cookie.getValue()) || StringUtils.isEmpty(redisCookieValue)) {
                //   设置ZuulResponse为false
                requestContext.setSendZuulResponse(false);
                //   设置请求状态码信息（401）
                requestContext.setResponseStatusCode(HttpStatus.UNAUTHORIZED.value());
            }
        }
        return null;
    }
}